package com.mybatisplus.socket_test;

import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;

/**
 * @Auther: GongXl
 * @Date: 2021/2/27 10:44
 * @Description:
 */
public class ClientSocketTest2 {
    @Test
    public void startSocket() throws IOException {
            //创建Socket，指定ip，port
            Socket socket = new Socket("127.0.0.1", 8989);
//获得键盘输入
            BufferedReader sin = new BufferedReader(new InputStreamReader(
                    System.in));
//        DataInputStream is = new DataInputStream(socket.getInputStream());

//获得基于Socket的输入流和输出流
        DataOutputStream os = new DataOutputStream(socket.getOutputStream());

           /* BufferedReader is = new BufferedReader(new InputStreamReader(
                    socket.getInputStream()));*/
            String readline = "";
//向服务器写数据
            while (!readline.equals("exit")) {
                readline = sin.readLine();
                System.out.println("客户端录入："+readline);
                os.writeUTF(readline);
                os.flush();
            }
        }
}